package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.ProductSettingAdapter;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.model.ProductSettingModel;
import com.tiantianyuegou.util.LoadingDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品栏目设置
 * Created by ivany on 2016/1/20.
 */
public class ProductProgramaActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private ImageView mImg_back;
    private TextView mTxt_title, mTxt_menu;
    private ListView mListView;
    private ProductSettingAdapter mAdapter;
    private List<ProductSettingModel> mData;
    private ArrayList<ProductSettingModel> mSelectList;
    private ArrayList<ProductSettingModel> mAlreadySelect;

    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_setting);
        initViews();
    }

    @Override
    public void findViewByIds() {
        mImg_back = (ImageView) findViewById(R.id.img_title);
        mTxt_title = (TextView) findViewById(R.id.text_title);
        mTxt_menu = (TextView) findViewById(R.id.text_menu);
        mListView = (ListView) findViewById(R.id.product_setting_list);
    }

    @Override
    public void initListener() {
        mListView.setOnItemClickListener(this);
        mImg_back.setOnClickListener(this);
        mTxt_menu.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAlreadySelect = (ArrayList<ProductSettingModel>) getIntent().getSerializableExtra("alreadySelect");
        mLoadingDialog = new LoadingDialog(this);
        mSelectList = new ArrayList<>();
        mTxt_title.setText("商品栏目设置");
        mTxt_menu.setVisibility(View.VISIBLE);
        mTxt_menu.setText("完成");
        mData = new ArrayList<>();
        mAdapter = new ProductSettingAdapter(this);
        mListView.setAdapter(mAdapter);
        getProductColumns();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ProductSettingModel model = mData.get(position);
        mData.get(position).setIsSelect(!model.isSelect());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_title:
                finish();
                break;
            case R.id.text_menu:
                for (int i = 0; i < mData.size(); i++) {
                    if (mData.get(i).isSelect()) {
                        mSelectList.add(mData.get(i));
                    }
                }
                Intent intent = new Intent();
                intent.putExtra("programList", mSelectList);
                setResult(Constant.SELECT_PROGRAM_RESULT, intent);
                finish();
                break;
        }
    }

    private void getProductColumns() {
        mLoadingDialog.setMsg("正在获取商品栏目列表");
        mLoadingDialog.show();
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.GET_PRODUCT_COLUMNS, null, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "get product columns response:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("暂无商品栏目信息");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            JSONArray list = root.getJSONArray("list");
                            if (list.length() > 0) {
                                mData.clear();
                                for (int i = 0; i < list.length(); i++) {
                                    JSONObject value = list.getJSONObject(i);
                                    ProductSettingModel model = new ProductSettingModel();
                                    model.setId(value.getInt("tcId"));
                                    model.setLabel(value.getString("tcName"));
                                    model.setPicUrl(value.getString("tcCode"));
                                    model.setType("3");
                                    if (mAlreadySelect != null && mAlreadySelect.size() > 0) {
                                        for (int j = 0; j < mAlreadySelect.size(); j++) {
                                            if (model.getId() == mAlreadySelect.get(j).getId()) {
                                                model.setIsSelect(true);
                                            }
                                        }
                                    } else {
                                        model.setIsSelect(false);
                                    }
                                    mData.add(model);
                                }
                                mAdapter.setData(mData);
                                mAdapter.notifyDataSetChanged();
                            } else {
                                showMsg("暂无商品栏目信息");
                            }

                        } else {
                            String msg = root.getString("message");
                            showMsg(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showMsg("数据解析异常");
                    }
                }


            }

            @Override
            public void onFailure(HttpException e, String s) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "get product columns failure:" + s);
                showMsg("当前网络不可用,请检查您的网络设置");
            }
        });
    }

}
